function connect(success,fail){
  wx.connectSocket({
    url: 'wss://card-87.zuanliantech.com/cable',
    header: {
      'content-type': 'application/json',
      "Origin": "http://admin-89.zuanliantech.com"
    },
    protocols: ["actioncable-v1-json", "actioncable-unsupported"],
    method: "GET"
  })

  wx.onSocketOpen(function (res) {
    console.log('WebSocket连接已打开！')
    let meat = {
      "channel": "RoomChannel",
      "label": "1st",
      "user_id": getApp().globalData.userId 
    }
    wx.sendSocketMessage({
      data: JSON.stringify({ "command": "subscribe", "identifier": JSON.stringify(meat) }),
    })
    success()
  })

  wx.onSocketError(function (res) {
    console.log('WebSocket连接打开失败，请检查！')
    fail()
  })

  wx.onSocketMessage(function (res) {
    let data = JSON.parse(res.data)
    if (data.type == "ping" || data.type == "welcome") {
      return
    }
    if (data.message) {
      console.log('收到服务器内容：' + data.message)
      let pages = getCurrentPages();
      let page = pages[pages.length - 1];
      if (data.message.type == "get_period_code") {
        console.log('获得密码：' + data.message)
        if (page.route == "pages/home/home" || page.route == "pages/happyJelly/jellyHome/jellyHome") {
          page.onReceive(data.message)
        }
      }
    }
  })
}
function send(param){
  let meat = {
  "channel":"RoomChannel",
  "label":"1st",
  "data": param,
  "user_id": getApp().globalData.userId 
  }
  wx.sendSocketMessage({
    data: JSON.stringify({ "command": "message", "identifier": JSON.stringify(meat) }),
  })

}

exports.connect = connect;
exports.send = send;